﻿///<reference path="../jquery/jquery.js"/>
var TopicList = function() {
	//private area
	var _ajaxDefaultOpts = {
		type: 'post',
		dataType: 'json',
		contentType: 'application/json;charset=utf-8',
		cache:false
	};
	var _doSave = function() {
		$("#btnSave").html("保存中...").attr("disabled", true);
		//get all inputs for category edit
		var data = $(":input").filter("[name^='item']").serializeArray();
		//Topic body
		var topicBody = {};
		topicBody.name = "itemContent";
		topicBody.value = tinyMCE.activeEditor.getContent();
		data.push(topicBody);
		data.push({ name: "itemCommunityID", value: TopicList.CommunityID });
		//requset to server
		var ajaxopts = _ajaxDefaultOpts;
		ajaxopts.url = TopicList.Asmx + "Update";
		ajaxopts.data = $.toJSON({ Params: data });
		ajaxopts.success = function(msg) {
			$("#btnSave").html("保	存").attr("disabled", false);
			msg = msg.d;
			if (msg.IsOk) {
				TopicList.Select($.evalJSON(TopicList.InitPager));
			} else {
				alert(msg.Body);
			}
		};
		$.ajax(ajaxopts);
		ajaxopts = null;
	};
	var _captchaCallback = function(arg, context) {
		if (arg == "1") {
			return;
		} else {
			_doSave();
		};
	};
	//public areas
	return {
		InitPager: "",
		Asmx: "",
		CommunityID: "",
		ViewPath: "~/Views/TopicListView.ascx",
		HasItems: true,
		Init: function(opts) {
			TopicList.Asmx = opts.Asmx || "";
			TopicList.CommunityID = opts.CommunityID || "";
			TopicList.InitPager = opts.InitPagerStr || "";
			TopicList.ViewPath = opts.ViewPath || TopicList.ViewPath;
			$("#btnSave").click(function() {
				TopicList.NewTopic({});
			});
			//Do first load.
			TopicList.Select($.evalJSON(TopicList.InitPager));
		}, //endof Init
		DefaultDialogOpts: {
			modal: true,
			width: 600,
			height: 400,
			resizable: false,
			overlay: { opacity: 0.5, background: 'black' }
		},
		PreEdit: function(id, opts) {
			if (id != -1) {
				//prepare ajax opts
				var ajaxOpts = _ajaxDefaultOpts;
				ajaxOpts.url = TopicList.Asmx + "PreEdit";
				ajaxOpts.data = $.toJSON({ Params: { id: id} });
				ajaxOpts.success = function(data) {
					data = data.d;
					$("#txtTitle").val(data.Title).select();
					$("#txtEMail").val(data.EMail);
					$("#txtName").val(data.Name);
					$("#txtDataKey").val(id);
					//Topic body
					tinyMCE.activeEditor.setContent(data.Content);
					if (opts.popup) {
						TopicList.DefaultDialogOpts.close = function() {
							$("#quickreplyboxWrapper").append($("#quickreplybox").dialog("destroy").remove());
						};
						$("#quickreplybox").dialog(TopicList.DefaultDialogOpts);
					};
				} //endof success

				//Request to server
				$.ajax(ajaxOpts);
				ajaxOpts = null;
			}
		}, //endof preEdit
		Del: function(opts) {
			var key = opts.id;
			if (!window.confirm("确认删除选定记录么?")) {
				return;
			};
			//Request to server
			var ajaxOpts = _ajaxDefaultOpts;
			ajaxOpts.url = TopicList.Asmx + "Delete";
			ajaxOpts.data = $.toJSON({ Params: { id: key} });
			ajaxOpts.success = function(data) {
				//ToDo:Remove A topic 
			}
			$.ajax(ajaxOpts);
			ajaxOpts = null;
		}, //endof delete
		NewTopic: function(opts) {
			//anti-messy reply
			WebForm_DoCallback('ctl00$cphBody$quickReply1', $("#hfCaptcha1").val(), _captchaCallback, 'topic', null, false);
		}, //Endof NewTopic
		Select: function(opts) {
			var data = {};
			data.CPage = opts.CurrentPage;
			data.RPPage = opts.RowPerPage;
			data.CommunityID = TopicList.CommunityID;
			data.ViewPath = TopicList.ViewPath;
			var ajaxOpts = _ajaxDefaultOpts;
			ajaxOpts.url = TopicList.Asmx + "SelectTopics";
			ajaxOpts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					//Replace TopicList
					$("#topicsWrapper").html(msg.Context.TopicListHtml);
					//Replace pager
					$("#pagerWrapper").html(msg.Context.PagerHtml);
					//Init pager again
					if (msg.Context.Pager) {
						SimplePager.Init(msg.Context.Pager, TopicList.Select);
					};
					//scroll to the latest record
					if (msg.Context.TopItemID) {
						var topItemDivID = "id_" + msg.Context.TopItemID; //topItemID was determined by TopicView.ascx.
						$.scrollTo("#" + topItemDivID);
					};
					TopicList.HasItems = msg.Context.HasItems;
				} else {
					alert("Something goes wrong!");
				}
			};
			ajaxOpts.data = $.toJSON({ Params: data });
			$.ajax(ajaxOpts);
		}, //endof Select
		Approve: function(opts) {
			var data = { TopicID: opts.id };
			var ajaxOpts = _ajaxDefaultOpts;
			ajaxOpts.url = TopicList.Asmx + "ApproveTopic";
			ajaxOpts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					//remove approve button
					$(opts.obj).hide();
				} else {
					alert("审核失败,请稍候重试");
				}
			};
			ajaxOpts.data = $.toJSON({ Params: data });
			$.ajax(ajaxOpts);
		} //endof Approve
	};
} ();